package com.ruoyi.MtTakeaway.config;

import org.apache.commons.codec.binary.Hex;

import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author zhangguangyan
 * @Date 2023/09/23/ 01:46:45
 */
public class getSign {
    /**
     * 解析url参数
     */
    public static TreeMap<String, String> parseQueryString(String urlString) {
        TreeMap<String, String> queryParams = new TreeMap<>();

        try {
            URL url = new URL(urlString);
            String queryString = url.getQuery();

            if (queryString != null && !queryString.isEmpty()) {
                String[] pairs = queryString.split("&");

                for (String pair : pairs) {
                    int idx = pair.indexOf("=");
                    String key = idx > 0 ? pair.substring(0, idx) : pair;
                    String value = idx > 0 && pair.length() > idx + 1 ? pair.substring(idx + 1) : null;
                    queryParams.put(key, value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return queryParams;
    }
    /**
     * 生成签名 获取sign
     *
     * @param params
     * @return
     */
    public static String genSign(TreeMap<String, String> params) {
        params.remove("sign");
        StringBuilder stringBuilder = new StringBuilder();
        // 替换为「我要推广」-「联盟API接口」页面下的签名秘钥值
        String apiToken = "3e1fcbe117bab0c34b2c0a79a8946167";
        stringBuilder
                .append(apiToken)
                .append(params.entrySet().stream()
                        .map(entry -> entry.getKey() + entry.getValue())
                        .collect(Collectors.joining()))
                .append(apiToken);
        System.out.println(stringBuilder.toString());
        return md5(stringBuilder.toString());
    }





    /**
     * @return
     */
    public static String md5(String s) {
        try {
            byte[] b = s.getBytes("UTF-8");
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.reset();
            md.update(b);
            byte[] hash = md.digest();
            StringBuffer outStrBuf = new StringBuffer(32);
            for (int i = 0; i < hash.length; i++) {
                int v = hash[i] & 0xFF;
                if (v < 16) {
                    outStrBuf.append('0');
                }
                outStrBuf.append(Integer.toString(v, 16).toLowerCase());
            }
            return outStrBuf.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return new String(s);
        }
    }
    public static String md52(String source) {
        String md5Result = null;
        try {
            byte[] hash = org.apache.commons.codec.binary.StringUtils.getBytesUtf8(source);
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(hash);
            hash = messageDigest.digest();
            md5Result = Hex.encodeHexString(hash);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return md5Result;
    }

    public static void main(String[] args) {
String aa ="https://openapi.meituan.com/api/mtunion/poi?appkey=57c7bea1cc39583f0f51fc5dcc8673cf&sid=wmfy001&businessLine=2&longitude=106629597&latitude=26683588&ts=1695405220522&sign=c2098355de4ae539d2965823891390d9&pageNo=1&pageSize=20&cateId=108419";
        TreeMap<String, String> stringStringTreeMap = parseQueryString(aa);
        String s = genSign(stringStringTreeMap);
        System.out.println(s);
    }
}
